package com.ssy.dao;

import com.ssy.common.PageInfo;
import com.ssy.entity.Course;
import com.ssy.vo.StudentVo;

import java.util.HashMap;

/**
 * CourseDao
 *
 * @author ssy
 * @date 2020/11/20
 **/
public class CourseDao extends BaseDao {

    public PageInfo<Course> selectPage(HashMap<String, Object> param, Integer pageNo, Integer limitNo) {
        String sql = "SELECT course_id AS courseId,number AS number,name AS name,class_hours AS classHours,\n" +
                "credit AS credit FROM course where";
        if (param.containsKey("number")) {
            sql = sql + " number = " + param.get("number") + "  and";
        }
        if (param.containsKey("name")) {
            sql = sql + " name like '%" + param.get("name") + "%'  and";
        }
        if (sql.endsWith("and")) {
            sql = sql.substring(0, sql.lastIndexOf("and"));
        }
        if (sql.endsWith("where")) {
            sql = sql.substring(0, sql.lastIndexOf("where"));
        }
        return this.selectPage(sql, Course.class, pageNo, limitNo);

    }


    public boolean insertCourse(int numberNo, String name, int classHoursNo, int creditNo) {
        String sql = "INSERT INTO course(number,name,class_hours,credit) VALUE(?,?,?,?)";
        int courseId = this.insert(sql, numberNo, name, classHoursNo, creditNo);
        return courseId > 0 ? true : false;
    }

    public boolean modifyCourse(int numberNo, String name, int classHoursNo, int creditNo, int courseIdNo) {
        String sql = "UPDATE course SET number=?,name=?,class_hours=?,credit=? WHERE course_id=?";
        return this.update(sql, numberNo, name, classHoursNo, creditNo, courseIdNo);
    }

    public boolean deleteCourse(int idNo) {
        String sql = "delete from course where course_id=?";
        return this.update(sql, idNo);
    }

    public boolean batchDeleteCourse(String[] ids) {
        String sql = "delete from course where course_id=?";
        return this.batchDelete(sql, ids);
    }

    public Course selectCourseByNumber(int numberNo) {
        String sql = "SELECT course_id AS courseId FROM course WHERE number=?";
        return this.selectOne(sql, Course.class, numberNo);
    }

    public Course selectCourseByName(String name) {
        String sql = "SELECT course_id AS courseId FROM course WHERE name=?";
        return this.selectOne(sql, Course.class, name);
    }

    public Course selectCourseById(int courseIdNo) {
        String sql = "SELECT number AS number,name AS name FROM course WHERE course_id=?";
        return this.selectOne(sql, Course.class, courseIdNo);
    }
}
